package org.example;

import org.example.dao.OrderDAO;
import org.example.dao.ProductDAO;
import org.example.dao.UserDAO;
import org.example.entity.Order;
import org.example.entity.OrderItem;
import org.example.entity.Product;
import org.example.entity.User;
import org.example.service.OrderService;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * @author xiaomengzi
 * @create 2025-08-04 下午1:50
 */
//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {
    public static void main(String[] args) {
        try{
            //初始数据对象
            UserDAO userDAO = new UserDAO();
            ProductDAO productDAO = new ProductDAO();
            OrderDAO orderDAO = new OrderDAO();
            OrderService orderService = new OrderService();

            //用户认证
            User user = userDAO.authenticate("testuser","password123");
            if (user!=null){
                System.out.println("用户认证成功"+user.getUsername());
                //显示所有产品
                List<Product>productList = productDAO.getAllProducts();
                for (Product product : productList) {
                    System.out.printf("名称：%s，描述：%s，价格：%s，库存：%d%n",
                            product.getName(), product.getDescription(),
                            product.getPrice(), product.getStock());
                }
                //创建订单并下单
                List<int[]> orderitems  =  new ArrayList<>();
                orderitems.add(new int[]{1,2});
                orderitems.add(new int[]{2,1});
                int orderid = orderService.createOrder(user.getId(),orderitems, user.getAddress());
                System.out.printf("订单创建成功，订单Id：%d%n",orderid);
                Order order = orderDAO.getOrder(orderid);
                if (order!=null){
                    System.out.printf("订单详情：%s%n",order);
                }
                //更新订单状态
                orderDAO.updateOrder(orderid,"paid");
                order = orderDAO.getOrder(orderid);
                if (order!=null){
                    System.out.printf("订单详情：%s%n",order);
                }
            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {

        }
    }
}